package tech.aistar.controller;


import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.dto.CheckQues;
import tech.aistar.dto.Wrong;
import tech.aistar.entity.*;
import tech.aistar.service.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/parper")
public class WrongtopicAction {
    @Autowired
    public IWrongtopicService wrongtopicService;

    @Autowired
    public ISinchoiceService sinchoiceService;

    @Autowired
    public IManchoiceService manchoiceService;

    @Autowired
    public ICompletionService completionService;

    @Autowired
    public IProgrammingService programmingService;


    @RequestMapping("/wrongtopicadd")
    public String Wrongtopicadd(HttpServletRequest req){
        //接收request作用域中的数据    json字符串格式
        String json = req.getParameter("checkQues");
        //将json字符串格式的数据转化为json字符数组格式    CheckQues.class是数组里面的数据是CheckQues类型
        List<CheckQues> checkQues = JSON.parseArray(json, CheckQues.class);
//        System.out.println(checkQues);
        Wrongtopic wrongtopic = new Wrongtopic();
//        System.out.println("checkQues.size()"+checkQues.size());
        for(int i = 0; i < checkQues.size(); i++){
            //如果是单选题
            if (checkQues.get(i).getQueType() == 1){
                System.out.println("单选题" + i);
                Long sinId =  checkQues.get(i).getQueId();//用户提交的单选题id
                String answerByid = sinchoiceService.findAnswerByid(sinId);//通过id找到答案
                String answer = checkQues.get(i).getAnswer();//用户的答案
                if (!answerByid.equals(answer)){//答案不正确
                    //将属猪
                    wrongtopic.setWrongParperId(checkQues.get(i).getParperId());//试卷号
                    wrongtopic.setWrongType(checkQues.get(i).getQueType());//题目类型
                    wrongtopic.setWrongQid(checkQues.get(i).getQueId());//题目id
                    wrongtopic.setWrongAnswer(checkQues.get(i).getAnswer());//用户答案
                    int i1 = wrongtopicService.insertWrongtopic(wrongtopic);//插入数据库



                }
            }
            //如果是多选题
            if (checkQues.get(i).getQueType() == 2){
                System.out.println("多选题");
                Long sinId =  checkQues.get(i).getQueId();//用户提交的多选题id
                String answerByid = manchoiceService.findAnswerByid(sinId);//通过id找到正确答案
                String answer = checkQues.get(i).getAnswer();//用户的答案
                if (!answerByid.equals(answer)){//答案不正确
//                    System.out.println("answerByid"+answerByid);
//                    System.out.println("answer"+answer);
                    wrongtopic.setWrongParperId(checkQues.get(i).getParperId());//试卷号
                    wrongtopic.setWrongType(checkQues.get(i).getQueType());//题目类型
                    wrongtopic.setWrongQid(checkQues.get(i).getQueId());//题目id
                    wrongtopic.setWrongAnswer(checkQues.get(i).getAnswer());//用户答案
                    int i1 = wrongtopicService.insertWrongtopic(wrongtopic);//插入数据库
                    System.out.println(i1);
                    System.out.println(wrongtopic);
                }
            }
            //如果是填空题
            if (checkQues.get(i).getQueType() == 3){
                System.out.println("填空题");
                Long sinId =  checkQues.get(i).getQueId();//用户提交的多选题id
                String answerByid = completionService.findAnswerByid(sinId);//通过id找到正确答案
                String answer = checkQues.get(i).getAnswer();//用户的答案
                if (!answerByid.equals(answer)){//答案不正确
                    wrongtopic.setWrongParperId(checkQues.get(i).getParperId());//试卷号
                    wrongtopic.setWrongType(checkQues.get(i).getQueType());//题目类型
                    wrongtopic.setWrongQid(checkQues.get(i).getQueId());//题目id
                    wrongtopic.setWrongAnswer(checkQues.get(i).getAnswer());//用户答案
                    int i1 = wrongtopicService.insertWrongtopic(wrongtopic);//插入数据库
                    System.out.println(i1);
                    System.out.println(wrongtopic);
                }
            }

        }

        return "login";
    }

    @RequestMapping("/wrong")
    public String Wrong(Model model , Long parperId){
        List<Wrongtopic> wrongtopicList = wrongtopicService.findByParperId(parperId);
        List<Wrong> wrongList = new ArrayList<>();//传回前端的集合

        for(int i = 0; i < wrongtopicList.size(); i++){
            Long wrongType = wrongtopicList.get(i).getWrongType();//错题类型
            Long wrongQid = wrongtopicList.get(i).getWrongQid();//错题编号
            String wrongAnswer = wrongtopicList.get(i).getWrongAnswer();//用户答案
            //如果是单选题
            if (wrongType == 1L){
                Sinchoice sinchoice = sinchoiceService.findById(wrongQid);
                Wrong wrong = new Wrong();
                wrong.setParperid(parperId);//试卷编号
                wrong.setWrongType("单选题");//题目类型
                wrong.setWrongStem(sinchoice.getSinStem());//题目内容
                wrong.setWrongAnswer(wrongAnswer);//用户答案
                wrong.setTrueAnswer(sinchoice.getSinAnswer());//正确答案
                wrongList.add(wrong);
            }
            //如果是多选题
            else if (wrongType == 2L){
                Manchoice manchoice = manchoiceService.findById(wrongQid);
                Wrong wrong = new Wrong();
                wrong.setParperid(parperId);//试卷编号
                wrong.setWrongType("多选题");//题目类型
                wrong.setWrongStem(manchoice.getManStem());//题目内容
                wrong.setWrongAnswer(wrongAnswer);//用户答案
                wrong.setTrueAnswer(manchoice.getManAnswer());//正确答案
                wrongList.add(wrong);
            }
            //如果是填空题
            else if (wrongType == 3L){
                Completion completion = completionService.findById(wrongQid);
                Wrong wrong = new Wrong();
                wrong.setParperid(parperId);//试卷编号
                wrong.setWrongType("填空题");//题目类型
                wrong.setWrongStem(completion.getComStem());//题目内容
                wrong.setWrongAnswer(wrongAnswer);//用户答案
                wrong.setTrueAnswer(completion.getComAnswer());//正确答案
                wrongList.add(wrong);
            }
            //如果是编程题
            else if (wrongType == 4L){
                Programming programming= programmingService.findById(wrongQid);
                Wrong wrong = new Wrong();
                wrong.setParperid(parperId);//试卷编号
                wrong.setWrongType("编程题");//题目类型
                wrong.setWrongStem(programming.getProStem());//题目内容
                wrong.setWrongAnswer(wrongAnswer);//用户答案
                wrong.setTrueAnswer(programming.getProAnswer());//正确答案
                wrongList.add(wrong);
            }
        }
        model.addAttribute("wrongList", JSON.toJSON(wrongList));
        return "wrong";
    }



}
